package com.qdairlines.config;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.support.AbstractMessageSource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;

import com.qdairlines.service.i18n.MessageResourceService;

/**
 * 从数据库获取国际化资源
 * @author pangyannan
 * @date 2017年6月8日
 * @company QingDao Airlines
 * @description
 */
public class MessageResource extends AbstractMessageSource implements ResourceLoaderAware {

    @SuppressWarnings("unused")
    private ResourceLoader resourceLoader;
    
    @Resource
    private MessageResourceService resourceService;
    
    /**
     * Map切分字符
     */
    protected final String MAP_SPLIT_CODE = "|";
    
    protected final String DB_SPLIT_CODE = "_";
    
    private final Map<String, String> properties = new HashMap<String, String>();
    
    public MessageResource() {
//        reload();
    }
    
    public void reload() {
        properties.clear();
        properties.putAll(loadTexts());
    }

    protected Map<String, String> loadTexts() {
        Map<String, String> mapResource = new HashMap<String, String>();
        List<com.qdairlines.entity.i18n.Resource> resources = resourceService.listData(new com.qdairlines.entity.i18n.Resource());
        for (com.qdairlines.entity.i18n.Resource item : resources) {
            String code = item.getName() + MAP_SPLIT_CODE + item.getLanguage();
            mapResource.put(code, item.getText());
        }
        return mapResource;
    }
    
    private String getText(String code, Locale locale) {
        String localeCode = locale.getLanguage() + DB_SPLIT_CODE + locale.getCountry();
        if(!"zh_CN".equals(localeCode) && !"en_US".equals(localeCode)){
        	localeCode ="zh_CN";
    	}
    	//String localeCode = locale.getLanguage() ;
        String key = code + MAP_SPLIT_CODE + localeCode;
        String localeText = properties.get(key);
        String resourceText = code;
        
        if(localeText != null) {
            resourceText = localeText;
        }
        else {
            localeCode = Locale.ENGLISH.getLanguage();
            key = code + MAP_SPLIT_CODE + localeCode;
            localeText = properties.get(key);
            if(localeText != null) {
                resourceText = localeText;
            }
            else {
                try {
                    if(getParentMessageSource() != null) {
                        resourceText = getParentMessageSource().getMessage(code, null, locale);
                    }
                } catch (Exception e) {
                    logger.error("Cannot find message with code: " + code);
                }
            }
        }
        return resourceText;
    }
    
    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = (resourceLoader != null ? resourceLoader : new DefaultResourceLoader());
    }

    @Override
    protected MessageFormat resolveCode(String code, Locale locale) {
        String msg = getText(code, locale);
        MessageFormat result = createMessageFormat(msg, locale);
        return result;
    }
    
    @Override
    protected String resolveCodeWithoutArguments(String code, Locale locale) {
        String result = getText(code, locale);
        return result;
    }

}